package cc.realxyq.demo.condition;

import cc.realxyq.demo.bean.Teacher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * 自定义ImportBeanDefinitionRegistrar
 * @author kwhan
 * @date 2022年01月08日 12:18
 */
@Slf4j
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        importingClassMetadata.getAnnotationTypes().forEach(log::warn);
        String beanName = "cc.realxyq.demo.bean.Red";
        //这里key增加业务逻辑按需注入bean
        if (registry.containsBeanDefinition(beanName)) {
            registry.removeBeanDefinition(beanName);
        }

        registry.registerBeanDefinition("MyTeacher", new RootBeanDefinition(Teacher.class));
    }
}
